# !/usr/bin/python
# -*- coding: UTF-8 -*-

import json
import datetime
import redis

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # 链接 redis
    redis_client = redis.Redis(host='localhost', port=6379)
    # 加载数据开始
    weibo_posts_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\Posts.json"
    weibo_comments_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\Comments.json"
    with open(weibo_posts_path, encoding="utf-8") as f:
        post_json_str = f.read()

    with open(weibo_comments_path, encoding="utf-8") as f:
        comment_json_str = f.read()

    posts_json = json.loads(post_json_str)
    comments_json = json.loads(comment_json_str)
    # 加载数据结束

    # 转化数据
    encoding = "utf-8"
    file_path = "D:\\mark-workspace\\corpus_data\\weibo\\CommentRInteractionDataset\\result_20251124.json"
    i = 0
    json_str = ""
    len_num = len(posts_json)
    print("len_num :  {0} !".format(len_num))

    # 对json数据中 retweet_id 进行排序, 使相同 retweet_id 的posts 聚集在一起
    posts_json = sorted(posts_json, key=lambda x: x.get("retweet_id", "0"))

    for post in posts_json:
        i = i + 1
        posts_mblog_id = post['mblogid']
        posts_content = post['content']
        uuid_key = "post:" + posts_mblog_id
        value = redis_client.get(uuid_key)
        print("i : {0}".format(i))
        if value is not None:
            print("{0} 已经处理完毕!".format(uuid_key))
            continue
        else:
            # 需要进行处理
            posts_comments_count = post['comments_count']
            # 等于0 的主题无意义，过滤掉
            if posts_comments_count > 0:
                posts_comments_list = []
                posts_source = "weibo"
                posts_created_at = datetime.datetime.strptime(post['created_at'], "%Y-%m-%d %H:%M:%S")
                posts_created_at_str = posts_created_at.date().strftime("%Y%m%d")
                posts_created_at_datetime_str = posts_created_at.date().strftime("%Y%m%d %H:%M:%S")
                posts_is_long_text = post['isLongText']
                posts_is_retweet = post['is_retweet']
                posts_user_id = post['user']['_id']
                if posts_is_retweet is False:
                    posts_retweet_id = 0
                else:
                    posts_retweet_id = post['retweet_id']

                results = [item for item in comments_json if item["root_post_mblogid"] == posts_mblog_id]
                content = {}
                for result_item in results:
                    # comment_extend = result_item
                    reply_comment_id = None
                    comment_user_id = result_item['comment_user']['_id']
                    comment_created_at = datetime.datetime.strptime(result_item['created_at'], "%Y-%m-%d %H:%M:%S")
                    comment_created_at_str = comment_created_at.date().strftime("%Y%m%d %H:%M:%S")
                    comment_extend = {
                        "created_at": comment_created_at_str,
                        "reply_comment_id": reply_comment_id,
                        "root_comment_id": result_item['root_comment_id'],
                        "root_post_mblogid": posts_mblog_id,
                        "comment_user_id": comment_user_id,
                    }
                    if "reply_comment" in result_item:
                        reply_comment_id = result_item["reply_comment"]['_id']
                    comment_item = {
                        "楼ID": result_item['_id'],
                        "回复": result_item['content'],
                        "扩展字段": comment_extend,
                    }
                    posts_comments_list.append(comment_item)
                posts_extends = {
                    "is_long_text": posts_is_long_text,
                    "is_retweet": posts_is_retweet,
                    "retweet_id": posts_retweet_id,
                    "user_id": posts_user_id,
                }
                content = {
                    "ID": posts_mblog_id,
                    "主题": posts_content,
                    "回复": posts_comments_list,
                    "时间": posts_created_at_str,
                    "元数据": {
                        "发帖时间": posts_created_at_datetime_str,
                        "回复数": posts_comments_count,
                        "扩展字段": posts_extends,
                    }
                }

                temp_str = json.dumps(content, ensure_ascii=False)
                json_str = json_str + temp_str + "\n"
                # with open(file_path, mode='a', encoding=encoding) as f:
                #     f.write(json_str)
            else:
                pass

            redis_client.set(uuid_key, 1)
            if i % 1000 == 0:
                print("w file1: {0}".format(i))
                with open(file_path, mode='a', encoding=encoding) as f:
                    f.write(json_str)
                json_str = ""

            if i == len_num:
                print("w file2: {0}".format(i))
                with open(file_path, mode='a', encoding=encoding) as f:
                    f.write(json_str)
                json_str = ""
    pass
